package juc.tool;

import java.util.concurrent.Exchanger;

public class ExchangerTest {
    public static void main(String[] args) throws InterruptedException {
        Exchanger<String> exchanger = new Exchanger<>();
        new Thread(() ->{
            try {
                System.out.println(Thread.currentThread().getName() + "发送交换消息" + exchanger.exchange("AAAAA"));
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        },"A").start();
        new Thread(() ->{
            try {
                System.out.println(Thread.currentThread().getName() + "发送交换消息" + exchanger.exchange("BBBB"));
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        },"B").start();
        String zhuzhu = exchanger.exchange("ZHUZHU");
        System.out.println("主线程" + zhuzhu);
        String zhuzhu2 = exchanger.exchange("ZHUZHU2");
        System.out.println("主线程" + zhuzhu2);

    }
}
